package com.ecommerce.dal.common;

import java.util.List;

import com.ecommerce.dal.entity.Communication;
import com.ecommerce.dal.entity.Company;
import com.ecommerce.dal.entity.CompanyCommunication;
import com.ecommerce.dal.entity.CompanyCommunicationId;
import com.ecommerce.dal.service.impl.CommunicationManagementService;
import com.ecommerce.dal.service.impl.CommunicationManagerService;
import com.ecommerce.dal.service.impl.CompanyManagerService;

public class TestCommunicationManagement {

	public TestCommunicationManagement(
			CommunicationManagementService communicationManagementService,
			CompanyManagerService companyManagerService,
			CommunicationManagerService communicationManagerService) {
		CompanyCommunication companyCommunication = new CompanyCommunication();
		Company company = companyManagerService.retrieveCompany(1);
		System.out.println("Loading Company " + company.getCompanyName()
				+ " Successfully");
		Communication communication = communicationManagerService
				.retrieveCommunication(1);
		System.out.println("Loading Communication "
				+ communication.getCommunicationType() + " Successfully");

		companyCommunication.setCommunication(communication);
		System.out.println("Not here");
		companyCommunication.setCompany(company);
		System.out.println("Neither here");

//
//		 companyManagerService.updateCompany(company);
//		 communicationManagerService.updateCommunication(communication);
		 System.out.println("Updated Company and Communication sets successfully!");
		/**build a Compound key manually: CompanyCommunicationId*/
		 
		 
		CompanyCommunicationId ccId = new CompanyCommunicationId();
		ccId.setCommunicationId(communication.getCommunicationId());
		ccId.setCompanyId(company.getCompanyId());
		ccId.setCompanyCommunicationDesc("01224205791");
		
		companyCommunication.setId(ccId);
		
		
		/**Make sure fetch.EAGER fetching for company and communication*/
		communicationManagementService
				.saveCompanyCommunication(companyCommunication);
		System.out.println("Saveing Company Named: " + company.getCompanyName()
				+ " Communication type " + communication.getCommunicationType()
				+ " now done!");

		List<CompanyCommunication> companyCommunicationList = communicationManagementService
				.retrieveAllCompanyCommunication(1L);
		for (CompanyCommunication companyCommunicationX : companyCommunicationList) {
			System.out.println("Company Name: "
					+ companyCommunicationX.getCompany().getCompanyName());
			System.out.println("Communication Id: "
					+ companyCommunicationX.getCommunication().getCommunicationType());
		}


	}

}
